let fs = require('fs').promises;
let path = require('path');

async function batchRenameDirectories(baseDir, renameFn) {
  try {
    // 读取指定目录的内容
    let items = await fs.readdir(baseDir, { withFileTypes: true });
    for (let item of items) {
      // 检查是否为目录
      if (item.isDirectory()) {
        // 构造旧文件夹路径
        let oldPath = path.join(baseDir, item.name);
        // 使用renameFn函数生成新的文件夹名称
        let newName = renameFn(item.name);
        let newPath = path.join(baseDir, newName);
        // 如果新旧名称不同，则进行重命名
        if (newName !== item.name) {
          await fs.rename(oldPath, newPath);
          console.log(`文件夹 "${item.name}" 已成功重命名为 "${newName}"`);
        }
      }
    }
  } catch (error) {
    console.error('发生错误:', error.message);
  }
}

// 定义一个函数用于生成新的文件夹名
function generateNewFolderName(oldFolderName) {
  let replacements = [
    { pattern: '多图下载｜', replacement: '' },
    { pattern: /[，？?…！~_♥!「」]/g, replacement: '' }, // 匹配多个标点符号
    { pattern: /《.*?》/g, replacement: '' },   // 匹配书名号及内容
    { pattern: /_-_/gi, replacement: '' },      // 匹配特殊符号
    { pattern: '話', replacement: '话' },       // 替换单个字符
    { pattern: '未删减版全集免費在线阅读', replacement: '' },
    { pattern: '未删减版免費阅读', replacement: '' },
    { pattern: '禁漫城', replacement: '' },
    { pattern: '18禁漫画天堂', replacement: '' },
    { pattern: '色漫谷', replacement: '' },
    { pattern: '---', replacement: '' },
    { pattern: '...', replacement: '' },
    { pattern: '--', replacement: '' },
  ];
  return replacements.reduce((name, { pattern, replacement }) => {
    return name.replace(pattern, replacement);
  }, oldFolderName);
}

// 调用函数，替换第一个参数为你的目标目录路径
batchRenameDirectories('D:\\HONOR Share\\Backup\\Docs', generateNewFolderName);
